﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace Code4WP7SDK
{
    public class IsoStorageHelper
    {
        public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        /// <summary>
        /// 保存键值对，保存之前要做校验
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SaveInToSettings(string key,object value) 
        {
            settings.Add(key, value);
            settings.Save();
        }

        /// <summary>
        /// 根据键获取值,该值可能是对象也可能是字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValueFromSettings(string key)
        {
            object value;
            settings.TryGetValue<object>(key,out value);
            return value;
        }

        /// <summary>
        /// 删除该键对应的值
        /// </summary>
        /// <param name="key"></param>
        public static void DeleteSettings(string key)
        {
            settings.Remove(key);
        }
        /// <summary>
        /// 删除所有信息
        /// </summary>
        public static void DeleteAllSettings()
        {
            foreach (string key in settings.Keys)
            {
                DeleteSettings(key);
            }
        }

        /// <summary>
        /// 获取IsolatedStrage中的所有对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetAllValues<T>()
        {
            List<T> result = new List<T>();
            foreach (string key in settings.Keys)
            {
                T value;
                settings.TryGetValue<T>(key, out value);
                result.Add(value);
            }
            return result;
        }
    }
}
